/**
 * 加载树
 * @param ztreeId
 * @param roleId
 */
function initZtree(ztreeId, roleId) {
	var setting = {
		async: {
			enable: true,
			url: "getZtree?roleId=" + roleId
		},
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true,
				idKey:'id',
				pIdKey:'parentId',
				rootPId:null
			}
		}
	};
	$.fn.zTree.init($("#"+ztreeId), setting);
}

/**
 * 将表单数据转为json格式
 * @param formId 表单ID
 * @returns
 */
function form2Json(formId) {
	var arr = $("#" + formId).serializeArray();
	var jsonStr = "";

	jsonStr += '{';
	for (var i = 0; i < arr.length; i++) {
		jsonStr += '"' + arr[i].name + '":"' + arr[i].value + '",';
	}
	jsonStr = jsonStr.substring(0, (jsonStr.length - 1));
	jsonStr += '}';

	var json = JSON.parse(jsonStr);
	return json;
}

/**
 * 加载地区
 */
function initArea() {
	$.post('queryArea', {}, function(data){
		for(var i=0; i<data.length; i++){
			$("#province").append($("<option value="+data[i].id+">"+data[i].name+"</option>"));
		}
    });
	
	$("#province").change(function(){
		$("#city option:not(:first)").remove();
		$("#area option:not(:first)").remove();
		var province =  $("#province option:selected").val();
		console.log(province);
		$.post('queryArea',{areaId:province},function(data){
			for(var i=0; i<data.length; i++){
				$("#city").append($("<option value="+data[i].id+">"+data[i].name+"</option>"));
			}
		});
	});
	$("#city").change(function(){
		$("#area option:not(:first)").remove();
		var areaId = $("#city option:selected").val();
		$.post('queryArea',{areaId:areaId},function(data){
			for(var i=0; i<data.length; i++){
				$("#area").append($("<option>"+data[i].name+"</option>"));
			}
		});
	});
}

/**
 * 加载时间
 * @param yearValue
 * @param monthValue
 * @param dayValue
 */
function initTime(yearValue, monthValue, dayValue) {
	var year = $("#year");
	var month = $("#month");
	var day = $("#day");

	/*初始化年*/
	var dDate = new Date();
	var dCurYear = dDate.getFullYear();
	var str = "";
	
	for (var i = dCurYear; i >= (dCurYear-100); i--) {
		str = "<option value=" + i + ">" + i + "</option>";
		year.append(str);
	}
	$("#year").find("option[value=" + yearValue + "]").attr("selected",true);
	
	/*初始化月*/
	for (var i = 1; i <= 12; i++) {
		str = "<option value=" + i + ">" + i + "</option>";
		month.append(str);
	}
	$("#month").find("option[value=" + monthValue + "]").attr("selected",true);
	
	/*调用函数初始化日*/
	TUpdateCal(year.val(), month.val());
	
	function TUpdateCal(iYear, iMonth) {
		var dDate = new Date();
		var daysInMonth = TGetDaysInMonth(iMonth, iYear);
		var str = "";
		$("#day").empty();
		for (var d = 1; d <= parseInt(daysInMonth); d++) {
			str = "<option value=" + d + ">" + d + "</option>";
			$("#day").append(str);
		}
		$("#day").find("option[value=" + dayValue + "]").attr("selected",true);
	}
	
	/*根据年月获取当月最大天数*/
	function TGetDaysInMonth(iMonth, iYear) {
		var dPrevDate = new Date(iYear, iMonth, 0);
		return dPrevDate.getDate();
	}
	
	$("#year,#month").bind("change", function(){
		TUpdateCal(year.val(),month.val());
	});
}

/**
 * 加载当前时间
 */
function initCurrTime() {
	var year = $("#year");
	var month = $("#month");
	var day = $("#day");

	/*初始化年*/
	var dDate = new Date();
	var dCurYear = dDate.getFullYear();
	var str = "";
	
	for (var i = dCurYear; i >= (dCurYear-100); i--) {
		str = "<option value=" + i + ">" + i + "</option>";
		year.append(str);
	}
	$("#year").find("option[value=" + dCurYear + "]").attr("selected",true);
	
	/*初始化月*/
	for (var i = 1; i <= 12; i++) {
		str = "<option value=" + i + ">" + i + "</option>";
		month.append(str);
	}
	$("#month").find("option[value=" + (dDate.getMonth() + 1) + "]").attr("selected",true);
	
	/*调用函数初始化日*/
	TUpdateCal(year.val(), month.val());
	
	function TUpdateCal(iYear, iMonth) {
		var dDate = new Date();
		var daysInMonth = TGetDaysInMonth(iMonth, iYear);
		var str = "";
		$("#day").empty();
		for (var d = 1; d <= parseInt(daysInMonth); d++) {
			str = "<option value=" + d + ">" + d + "</option>";
			$("#day").append(str);
		}
		$("#day").find("option[value=" + dDate.getDate() + "]").attr("selected",true);
	}
	
	/*根据年月获取当月最大天数*/
	function TGetDaysInMonth(iMonth, iYear) {
		var dPrevDate = new Date(iYear, iMonth, 0);
		return dPrevDate.getDate();
	}
	
	$("#year,#month").bind("change", function(){
		TUpdateCal(year.val(), month.val());
	});
}
